/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import me.zhengjie.modules.system.domain.PokerServer;
import me.zhengjie.modules.system.domain.PokerUserServerInfo;
import me.zhengjie.modules.system.domain.User;
import me.zhengjie.modules.system.service.PokerUserServerInfoService;
import me.zhengjie.modules.system.service.UserService;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.system.service.PokerServerService;
import me.zhengjie.modules.system.domain.dto.PokerServerQueryCriteria;
import me.zhengjie.modules.system.mapper.PokerServerMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author greazy
* @date 2025-04-10
**/
@Service
@RequiredArgsConstructor
public class PokerServerServiceImpl extends ServiceImpl<PokerServerMapper, PokerServer> implements PokerServerService {

    private final PokerServerMapper pokerServerMapper;

    private final PokerUserServerInfoService userServerInfoService;

    private final UserService userService;




    @Override
    public PageResult<PokerServer> queryAll(PokerServerQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(pokerServerMapper.findAll(criteria, page));
    }

    @Override
    public List<PokerServer> queryAll(PokerServerQueryCriteria criteria){
        return pokerServerMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(PokerServer resources) {
        pokerServerMapper.insert(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PokerServer resources) {
        PokerServer pokerServer = getById(resources.getId());
        pokerServer.copy(resources);
        pokerServerMapper.updateById(pokerServer);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        pokerServerMapper.deleteBatchIds(ids);
    }

    @Override
    public void download(List<PokerServer> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PokerServer pokerServer : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("服务器名称", pokerServer.getName());
            map.put("IP地址", pokerServer.getIpAddr());
            map.put("端口", pokerServer.getPortAddr());
            map.put("rcon连接密码", pokerServer.getPasswordRcon());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public void dataSync() {
        List<User> userList = userService.list();
        List<PokerServer> pokerServerList = this.list();
        ArrayList<PokerUserServerInfo> insertUserServerInfoList = new ArrayList<>();
        for (PokerServer pokerServer : pokerServerList) {
            for (User user : userList) {
                LambdaQueryWrapper<PokerUserServerInfo> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(PokerUserServerInfo::getUserId,user.getId()).eq(PokerUserServerInfo::getServerId,pokerServer.getId());
                PokerUserServerInfo one = userServerInfoService.getOne(wrapper);
                if (ObjectUtil.isEmpty(one)){
                    PokerUserServerInfo userServerInfo = new PokerUserServerInfo();
                    userServerInfo.setUserId(user.getId());
                    userServerInfo.setServerId(pokerServer.getId());
                    userServerInfo.setJettonNum(0);
                    insertUserServerInfoList.add(userServerInfo);
                }

            }
        }
        userServerInfoService.saveBatch(insertUserServerInfoList);
    }
}